<?php

require_once(SRC_PATH."Object/Verb.php");
require_once("GenericReader.php");

class VerbReader extends GenericReader{
		
	public function __construct($object = NULL, $connection = NULL){
		parent::__construct($object, $connection);
	}
	
	public function execute(){
		$this->select = new RXSelect($this->connection);
		
		switch($this->operation){
			case "GET_NEXT_ID": return $this->executeGetNextId();
			case "GET_ONE_BY_INFINITIVE": return $this->executeGetOneByInfinitive();
			case "GET_DUPLICATES": return $this->executeGetDuplicates();
			default:
				return $this->executeGetOne();			
		}
	}
	
	public function executeGetDuplicates(){
		$query = "SELECT verbs.*, COUNT(*) FROM verbs GROUP BY verb HAVING COUNT(*) > 1";
		$this->select->setQuery($query);
		return $this->select->execute();
	}
	
	public function executeGetOneByInfinitive(){
		$this->select->addTable("verbs");
		$this->select->addWhereAndStatementEquals("verb", $this->object->getVerb());		
		return $this->select->execute();
	}
	
	public function executeGetNextId(){
		$query = "SHOW TABLE STATUS LIKE 'verbs'";
		$this->select->setQuery($query);
		return $this->select->execute();
	}
	
	public function executeGetOne(){
		$this->select->addTable("verbs");
		$this->select->addWhereAndStatementEquals("id", $this->object->getId());
		return $this->select->execute();
	}
	
	public function next(){
		$row = $this->select->next();
		if($row){
			return new Verb(	$row["id"], 
											$row["verb"],
											($row["is_reflexive"] == '1') ? TRUE : FALSE);
		}else{
			return FALSE;
		}
	}
	
	public function getNextId(){
		$row = $this->select->next();
		return $row["Auto_increment"];
	}
}